//
//  XQHelper.m
//  JoinPeso
//
//  Created by 小琦 on 2025/1/20.
//

#import "XQHelper.h"
#import <AVFoundation/AVFoundation.h>
#import <Photos/Photos.h>
#import <sys/socket.h>
#import <sys/sockio.h>
#import <sys/ioctl.h>
#import <sys/mount.h>
#import <net/if.h>
#import <arpa/inet.h>
#include <sys/sysctl.h>
#include <mach/mach.h>

@implementation XQHelper

+ (NSString *)getTotalMemorySize {
    long long totalMemorySize = [NSProcessInfo processInfo].physicalMemory;
    return [self fileSizeToString:totalMemorySize];
}

+ (NSString *)getAvailableMemorySize {
    vm_statistics_data_t vmStats;
    mach_msg_type_number_t infoCount = HOST_VM_INFO_COUNT;
    kern_return_t kernReturn = host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&vmStats, &infoCount);
    if (kernReturn != KERN_SUCCESS) {
        return @"内存查找失败";
    }
    long long availableMemorySize = ((vm_page_size * vmStats.free_count + vm_page_size * vmStats.inactive_count));
    return [self fileSizeToString:availableMemorySize];
}

+ (NSString *)getTotalDiskSize {
    struct statfs buf;
    unsigned long long totalDiskSize = -1;
    if (statfs("/var", &buf) >= 0) {
        totalDiskSize = (unsigned long long)(buf.f_bsize * buf.f_blocks);
    }
    return [self fileSizeToString:totalDiskSize];
}

+ (NSString *)getNewFreeSize {
    NSError *error = nil;
        NSDictionary *results = [[[NSURL alloc] initFileURLWithPath:NSTemporaryDirectory()] resourceValuesForKeys:@[NSURLVolumeAvailableCapacityForImportantUsageKey] error:&error];
        if(error){return @"-1";}
    
        return [NSString stringWithFormat:@"%@",results[NSURLVolumeAvailableCapacityForImportantUsageKey]];
}

+ (NSString *)fileSizeToString:(unsigned long long)fileSize {
    NSInteger KB = 1024;
    NSInteger MB = KB*KB;
    NSInteger GB = MB*KB;
    
    return [NSString stringWithFormat:@"%llu",(fileSize)];
}

@end
